//
//  HMRetweetView.swift
//  WeiBo
//
//  Created by Feng on 2017/6/8.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class HMRetweetView: UIView {

    //约束属性
    var retweetViewBottomContraint: Constraint?
    
    var statusCellViewModel: HMStatusCellViewModel?
    {
        didSet {
            //判断statusCellViewModel
            guard let retweekViewModel = statusCellViewModel else
            {
                return
            }
            //转发微博内容
            contentLabel.text = retweekViewModel.reweetContent
            contentLabel.attributedText = statusCellViewModel?.retweetAttributedText
            
            //转发微博配图
            //先卸载约束
            retweetViewBottomContraint?.deactivate()
            
            //要根据是否有图片进行判断
            if let picUrls = retweekViewModel.status?.retweeted_status?.pic_urls, picUrls.count > 0
            {
                pictureView.isHidden = false
                
                pictureView.picUrls = retweekViewModel.status?.retweeted_status?.pic_urls
                
                //重新创建约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomContraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
            }
            else
            {
                pictureView.isHidden = true
                
                //重新创建约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomContraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: --懒加载控件
    //转发内容
//    private lazy var contentLabel: UILabel =
//    {
//        let label = UILabel()
//        
//        label.text = "UIKit中默认为我们准备好了一个图形上下文,在drawRect:( 这个方法在viewDidLoad之后调用 )中通过UIKit封装函数UIGraphicsGetCurrentContext( ) 获取."
//        label.numberOfLines = 0
//        label.textColor = UIColor.lightGray
//        label.font = UIFont.systemFont(ofSize: 13)
//        
//        return label
//    }()
    
    //使用YYText中的YYLabel 创建contentLabel
    private lazy var contentLabel: YYLabel =
    {
        let label = YYLabel()
        //设置最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        label.numberOfLines = 0
        return label
    }()
    
    //转发配图
    private lazy var pictureView: HMPictureView =
    {
        let picView = HMPictureView()
        
        return picView
    }()
    
    //MARK: --添加控件,设置约束
    func setupUI()
    {
        addSubview(contentLabel)
        addSubview(pictureView)
        
        pictureView.backgroundColor = backgroundColor
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //设置优先级,可以覆盖相同的约束
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        self.snp.makeConstraints { (make) in
            //记录约束属性,因为有转发数据,但是数量为0 的情况
            retweetViewBottomContraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        //  设置高亮点击闭包
        contentLabel.highlightTapAction = { (containerView, attributedText, range, rect) in
            //  containerView: 点击的视图contentLabel
            //  attributedText: 点击视图的富文本
            //  range: 点击高亮富文本的范围
            //  rect: 点击高亮的区域
            //  获取高亮的富文本
            let subAttributedText = attributedText.attributedSubstring(from: range)
            //  获取对应高亮富文本的字符串
            let text = subAttributedText.string
            print(text)
        }
        
    }
}
